package org.itboys.mobile.controller.web.income;

import com.wordnik.swagger.annotations.Api;
import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mdl.conmon.Finals;
import org.itboys.mobile.entity.mysql.income.MdlIncomeCashed;
import org.itboys.mobile.entity.mysql.income.MdlIncomeDetail;
import org.itboys.mobile.service.mysql.web.income.MdlWebIncomeService;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 作者：zzq
 * 时间：2017/12/25
 * 描述：迈迪龙_后台_收益
 */
@Controller
@RequestMapping("/mdl/web/income")
public class MdlWebIncomeController extends BaseController{
    @Autowired
    private MdlWebIncomeService incomeService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request,
                        @RequestParam(value="returnPage",required=true) Integer returnPage,
                                Model model){
        if(returnPage==1){
            return "/maidilong/income/cashed";
        }
        return "error";
    }

    /**
     * 获取提现记录
     * @param request
     * @param response
     * @param memberId
     * @param status
     */
    @RequestMapping("/getIncomeCashed")
    public void getIncomeCashed(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(value="memberId",required=false)Long memberId,
                               @RequestParam(value="status",required=false)Integer status
                               ){
        try{
            PageResult<MdlIncomeCashed> result=incomeService.getIncomeCashed(request,memberId,status);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 获取收益明细
     * @param request
     * @param response
     * @param memberId
     */
    @RequestMapping("/getIncomeRecord")
    public void getIncomeRecord(HttpServletRequest request,
                               HttpServletResponse response,
                               @RequestParam(value="memberId",required=false)Long memberId
    ){
        try{
            PageResult<MdlIncomeDetail> result=incomeService.getIncomeRecord(request,memberId);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        }catch (Exception e){
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    /**
     * 同意提现
     * @param request
     * @param response
     * @param cashedId
     * @param payNo
     */
    @RequestMapping("/agreeApply")
    public void agreeApply(HttpServletRequest request,
                     HttpServletResponse response,
                     @RequestParam(value="cashedId",required=true)Long cashedId,
                     @RequestParam(value="payNo",required=true)String payNo
                     ){
        try{
            incomeService.agreeApply(cashedId,payNo);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                msg=e.getMessage();
            }else{
                msg= Finals.ERROMSG;
                logger.error("/mdl/web/income/agreeApply/------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }

    /**
     * 拒绝提现
     * @param request
     * @param response
     * @param cashedId
     * @param remark
     */
    @RequestMapping("/disAgreeApply")
    public void disAgreeApply(HttpServletRequest request,
                     HttpServletResponse response,
                     @RequestParam(value="cashedId",required=true)Long cashedId,
                     @RequestParam(value="remark",required=true)String remark
    ){
        try{
            incomeService.disAgreeApply(cashedId,remark);
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.SUCCESS_CODE, CommonConstants.SUCCESS));
        }catch (Exception e){
            String msg ="";
            if(e.getClass().getSimpleName().equalsIgnoreCase("ErroException")){
                msg=e.getMessage();
            }else{
                msg= Finals.ERROMSG;
                logger.error("/mdl/web/income/disAgreeApply/------"+e.getMessage());
            }
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
}
